Apache POI :http://poi.apache.org/
http://poi.apache.org/download.html
解析xls格式的包:
poi-3.9-20121203.jar
解析xlsx格式的包:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
xmlbeans-2.3.0.jar
poi-ooxml-schemas-3.9-20121203.jar
dom4j-1.6.1.jar
- package cjw;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
-
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-
- public class ReadFile {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自动生成方法存根
- File file = new File("G:/aaa/01.xls");
- if(!file.exists()){
- System.out.println("文件不存在");
- return;
- }
- ReadFile rf = new ReadFile();
- rf.readExcel(file);
- }
-
- /**
- * 读取Excel数据
- * @param file
- */
- public void readExcel(File file){
- try {
- InputStream inputStream = new FileInputStream(file);
- String fileName = file.getName();
- Workbook wb = null;
- if(fileName.endsWith("xls")){
- wb = new HSSFWorkbook(inputStream);//解析xls格式
- }else if(fileName.endsWith("xlsx")){
- wb = new XSSFWorkbook(inputStream);//解析xlsx格式
- }
- Sheet sheet = wb.getSheetAt(0);//第一个工作表
-
- int firstRowIndex = sheet.getFirstRowNum();
- int lastRowIndex = sheet.getLastRowNum();
- for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
- Row row = sheet.getRow(rIndex);
- if(row != null){
- int firstCellIndex = row.getFirstCellNum();
- int lastCellIndex = row.getLastCellNum();
- for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){
- Cell cell = row.getCell(cIndex);
- String value = "";
- if(cell != null){
- value = cell.toString();
- System.out.print(value+"\t");
- }
- }
- System.out.println();
- }
- }
- } catch (FileNotFoundException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- }